import {balancePayment, weChatPayment} from "../api";

class Payments {

    static PAY_WAY_ENUM = {
        WX: 'weixin',
        BALANCE: 'balance'
    }

    //获取支付的参数
    async getPaymentParams(order_num) {
        try {
            const res = await weChatPayment({order_num})
            if (res.data.level == "success") {
                return res.data.data
            } else {
                throw new Error(res.data.message)
            }
        } catch (e) {
            throw new Error(e.message || '获取支付参数失败')
        }

    }

    //微信支付
    async payByWeiXin(order_num, order_id) {
        try {
            //获取支付参数
            const paymentParams = await this.getPaymentParams(order_num)
            //支付
            await this.payment(paymentParams,order_id)
        } catch (e) {
            wx.showModal({
                title: '获取支付参数失败',
                content: e.message,
                showCancel: false,
                confirmText: '确定',
                success(res) {
                    Payments.redirectToOrderDetail(order_id)
                }
            })
        }
    }

    //余额支付
    async payByBalance(order_num,order_id) {
        try {
            const res = await balancePayment({order_num: order_num + ''}) //TODO　这里传过来的order_num是一个整形，但是传参时使用了错误，所以这里要把他船政字符
            if (res.data.level == 'success') {
                wx.showToast({
                    title: res.data.message,
                    icon: 'success',
                    duration: 1500
                })
            } else {
                wx.showToast({
                    title: res.data.message,
                    icon: 'icon',
                    duration: 1500
                })
            }
        } catch (e) {

        } finally {
            setTimeout(() => {
                Payments.redirectToOrderDetail(order_id)
            }, 1500)
        }
    }

    showBalancePayModal(order_num,order_id,pay_amount) {
        const _this = this
        wx.showModal({
            title: '余额支付',
            content: `是否支付${pay_amount}元`,
            showCancel: true,
            confirmText: '确定',
            success(res) {
                if (res.confirm) {
                    _this.payByBalance(order_num, order_id)
                } else if (res.cancel) {
                    Payments.redirectToOrderDetail(order_id)
                }

            }
        })
    }

    async payment(paymentParams,order_id) {
        try {
            await wx.requestPayment(paymentParams);
            wx.showToast({
                title: '支付成功',
                icon: 'success',
                duration: 1500
            })
        } catch (e) {
            console.log('执行了吗')
            wx.showToast({
                title: '支付失败',
                icon: 'none',
                duration: 1500
            })
        } finally {
            setTimeout(() => {
                Payments.redirectToOrderDetail(order_id)
            }, 1500)
        }
    }

    static redirectToOrderDetail(order_id) {
        wx.redirectTo({
            url: `/pages/order-detail/order-detail?order_id=${order_id}`
        })
    }

}

export {
    Payments
}
